//
//  RankingViewController.m
//  GSG
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RankingViewController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "RankingCell.h"
#import "RankSonListViewController.h"
#import"PlayListViewController.h"
#import "MBProgressHUD.h"
@interface RankingViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)NSMutableArray *modelArr;
@property (nonatomic,retain)UITableView *rankTableView;
@property(nonatomic,retain)MBProgressHUD *HUD;
@end

@implementation RankingViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"排行";
        self.modelArr = [NSMutableArray array];
    }

    return self;
}
- (void)data{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=281&app=ttpod&v=v7.7.0.2015012818&uid=&mid=iPhone5S&f=f320&s=s310&imsi=&hid=&splus=7.1&active=1&net=2&openudid=f6194442ca8980c5d85b989cf3d3dc0a4377fda1&idfa=5AA671CE-591C-4ED9-9D2B-A3E90383B9F1&utdid=VTDTxsGQAW8DAMDeSyjEEAeS&alf=201200" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSDictionary *dict = responseObject;
        
        NSMutableArray *arr = [dict objectForKey:@"data"];
        for (NSInteger i=0; i<arr.count; i++) {
            
            MusicModel *movie = [[MusicModel alloc] init];
            NSMutableDictionary *movieDic = arr[i];
            [movie setValuesForKeysWithDictionary:movieDic];
            [self.modelArr addObject:movie];
            [movie release];

            
        }
        [self.rankTableView reloadData];
        [_HUD hide:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"No");
        NSLog(@"%@",error);
    }];
}

-(void)actionBuLand{
    PlayListViewController *plvc=[[PlayListViewController alloc]init];
    [self.navigationController pushViewController:plvc animated:YES];
    [plvc release];
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"播放" style:UIBarButtonItemStyleDone target:self action:@selector(actionBuLand)];
    

    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bujing.png"]];
    
        [self data];
    self.rankTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-self.view.frame.size.height * 50 / 667) style:UITableViewStylePlain];
    self.rankTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.rankTableView];
    self.rankTableView.rowHeight = 140;

    self.rankTableView.dataSource = self;
    self.rankTableView.delegate = self;

    //小菊花
    //方式1.直接在View上show
    _HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];

    [_rankTableView release];

    
    
    //常用的设置
    //小矩形的背景色
    _HUD.color = [UIColor clearColor];//这儿表示无背景
    //显示的文字
    _HUD.labelText = @"提示";
    //细节文字
    _HUD.detailsLabelText = @"正在加载内容";
    //是否有庶罩
    _HUD.dimBackground = YES;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.modelArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse = @"reuse";
    RankingCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[RankingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    MusicModel *music = self.modelArr[indexPath.row];
    NSURL *url = [NSURL URLWithString:music.pic_url];
    [cell.pic_urlView sd_setImageWithURL:url];
    
    cell.titleLabel.text = music.title;
    
    NSString *str = [NSString stringWithFormat:@"1.%@",music.songlist[0][@"songName"]];
    cell.songNameLabel.text = str;
    
    NSString *str1 = [NSString stringWithFormat:@"2.%@",music.songlist[1][@"songName"]];
    cell.songNameLabel1.text = str1;

    NSString *str2 = [NSString stringWithFormat:@"3.%@",music.songlist[2][@"songName"]];
    cell.songNameLabel2.text = str2;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RankSonListViewController *rank = [[RankSonListViewController alloc] init];
    [self.navigationController pushViewController:rank animated:YES];
    rank.musicModel = self.modelArr[indexPath.row] ;
    [rank release];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
